<template>
	<view class="con-box">
		<u-navbar title="" height="120upx">
			<view class="u-nav-slot" slot="left">
				<view @tap="back" class="icon-left">
					<u-icon name="arrow-left" color="#000" size="26"></u-icon>
				</view>
			</view>
			<view class="u-nav-slot logoBox" slot="center">
				<view class="navBox">修改登录密码</view>
			</view>
			<view class="u-nav-slot" slot="right">
				<view class="icon-right">
				</view>
			</view>
		</u-navbar>

		<view class="form">
			<u--form labelPosition="left" ref="form">
				<u-form-item :border-bottom="false">
					<view class="formInput">
						<view class="label">旧密码</view>
						<image @tap="passFlag1 = !passFlag1" class="iconfont1" mode="aspectFit"
							:src="passFlag1?require('@/static/login/kejian.png'):require('@/static/login/nkejian.png')">
						</image>
						<input placeholder-class="placeholderStyle" placeholder="请输入旧密码" maxlength="15"
							v-model="form.oldPassWord" class="inputStyle" :password="passFlag1?false:true" />
					</view>
				</u-form-item>
				<u-form-item :border-bottom="false">
					<view class="formInput">
						<view class="label">新密码</view>
						<image @tap="passFlag = !passFlag" class="iconfont1" mode="aspectFit"
							:src="passFlag?require('@/static/login/kejian.png'):require('@/static/login/nkejian.png')">
						</image>
						<input placeholder-class="placeholderStyle" placeholder="请输入8-15位字母+数字组合的密码" maxlength="15"
							v-model="form.newPassWord" class="inputStyle" :password="passFlag?false:true" />
					</view>
				</u-form-item>
				<u-form-item :border-bottom="false">
					<view class="formInput">
						<view class="label">重复密码</view>
						<image @tap="passFlag2 = !passFlag2" class="iconfont1" mode="aspectFit"
							:src="passFlag2?require('@/static/login/kejian.png'):require('@/static/login/nkejian.png')">
						</image>
						<input placeholder-class="placeholderStyle" placeholder="请确认新密码" maxlength="15"
							v-model="form.reNewPassWord" class="inputStyle" :password="passFlag2?false:true" />
					</view>
				</u-form-item>
				<view class="forgetPassword" @tap="goForgetPassword">忘记密码？</view>
			</u--form>
		</view>
		<view class="submit-bottom">
			<view class="submit-btn" @tap="authentication">确认修改</view>
		</view>
		<u-toast ref="uToast"></u-toast>
		<mix-loading ref="min_loading"></mix-loading>
	</view>
</template>

<script>
	import {
		updatePassword
	} from '../../api/user.js'
	import {
		logout,
	} from '@/api/login.js'
	export default {
		data() {
			return {
				passFlag: false,
				passFlag1: false,
				passFlag2: false,
				form: {
					oldPassWord: "",
					newPassWord: "",
					reNewPassWord: "",
				},
			}
		},
		methods: {
			authentication() {
				let obj = this.form;
				let reg = /^(?=.*\d)(?=.*[a-zA-Z])[\da-zA-Z~!@#$%^&*.]{8,15}$/;
				if (!reg.test(obj.oldPassWord)||!reg.test(obj.newPassWord)) {
					this.$u.toast('密码必须是由8-15位字母+数字组合');
				} else if (obj.reNewPassWord !== obj.newPassWord) {
					this.$u.toast('您输入的两次密码不一致');
				} else {
					this.$refs.min_loading.open('正在修改！');
					updatePassword({
						newPassWord: this.hrymd5.md5(obj.newPassWord),
						oldPassWord: this.hrymd5.md5(obj.oldPassWord),
						reNewPassWord: this.hrymd5.md5(obj.reNewPassWord),
					}).then(res => {
						if (res.data.success) {
							this.$u.toast('密码修改成功,请重新登录')
							setTimeout(() => {
								this.outLogin();
							}, 1000);
						} else {
							this.$u.toast(res.data.msg);
						}
					}).finally(() => {
						this.$refs.min_loading.close();
					});
				}
				// this.$refs.min_loading.open('正在修改！');
			},
			// 退出登录
			outLogin() {
				logout().then(res => {
					if (res.data.success) {
						this.$store.dispatch("OutLogin");
						uni.reLaunch({
							url: "/pages/login/login"
						});
					}
				})
			},
			back() {
				let canNavBack = getCurrentPages();
				if (canNavBack && canNavBack.length > 1) {
				 uni.navigateBack({
						delta: 1
					});
				} else {
					history.back();
				}
			},
			goForgetPassword(){
				uni.navigateTo({
					url:'/pages/login/forgotPassword'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.con-box {
		min-height: 100vh;
		padding-top: 120upx;

		.logoBox {
			width: 100%;
			height: 100%;

			.navBox {
				height: 120upx;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #000;
				box-shadow: 0px 0px 1px 0px rgba(51, 51, 51, 0.5000);
				font-size: 32upx;
				font-family: PingFang SC;
				font-weight: bold;
			}
		}

		.form {

			/deep/.u-form-item__body {
				padding: 0;
			}

			.formInput {
				width: 100%;
				border-bottom: 1px solid rgba($color: #C8C8C9, $alpha: 0.15);
				padding: 40upx 32upx;
				display: flex;
				align-items: center;
				flex-wrap: wrap;
				position: relative;

				.iconfont1 {
					position: absolute;
					bottom: 40upx;
					right: 40upx;
					width: 36upx;
					height: 36upx;
					z-index: 3;
				}

				.label {
					width: 100%;
					font-size: 30upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #333333;
					margin-bottom: 20upx;
				}

				.inputStyle {
					color: #333;
				}

				input {
					flex: 1%;
					font-size: 26upx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #AEAEAE;
				}
			}
			
			.forgetPassword{
				padding: 0 30upx;
				font-size: 26upx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #CEA539;
				text-align: right;
				padding-top: 20upx;
			}

			.imgCodeBox {
				.imgCode {
					height: 100%;
					display: flex;
					align-items: flex-end;
					align-content: flex-end;
					justify-content: space-between;

					image {
						width: 200upx;
						height: 60upx;
						position: absolute;
						bottom: 20upx;
						right: 0;
					}

					input {
						flex: 1%;
						padding-right: 30upx;
					}
				}
			}

			.codeBox {
				.label {
					width: 180upx;
				}

				.send-con {
					width: 100%;
					display: flex;
					justify-content: space-between;
					padding-right: 20upx;

					.send {
						display: inline-block;
						color: #2C1769;
						font-size: 24upx;
						width: 192upx;
						height: 60upx;
						background: #2C1769;
						border-radius: 30upx;
						color: #fff;
						font-size: 30upx;
						text-align: center;
						line-height: 60upx;
						position: absolute;
						bottom: 20upx;
						right: 0;
					}

					input {
						padding-right: 30upx;
					}
				}
			}
		}

		.submit-bottom {
			width: 100%;
			padding: 0 58upx;
			padding-top: 240upx;
		
			.submit-btn {
				width: 100%;
				height: 96upx;
				background: #CEA539;
				border-radius: 10upx;
				font-size: 32upx;
				font-weight: bold;
				color: #FFFFFF;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>
<style scoped>
	page {
		background-color: #fff;
	}
</style>
